package com.itheima.web.controller;

import com.itheima.domain.Syslog;
import com.itheima.service.*;
import com.itheima.service.impl.*;
import com.itheima.service.OrderService;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import com.itheima.service.SyslogService;
import com.itheima.service.UserService;
import com.itheima.service.impl.OrderServiceImpl;
import com.itheima.service.impl.PermissionServiceImpl;
import com.itheima.service.impl.RoleServiceImpl;
import com.itheima.service.impl.SyslogServiceImpl;
import com.itheima.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class BaseServlet extends HttpServlet {
    protected SyslogService syslogService;
    protected OrderService orderService;
    protected RoleService roleService;
    protected UserService userService;
    protected PermissionService permissionService;
    protected ProductService productService;

    protected ProductServlet productServlet;

    @Override
    public void init() throws ServletException {
        userService = new UserServiceImpl();
        syslogService = new SyslogServiceImpl();
        orderService = new OrderServiceImpl();
        roleService = new RoleServiceImpl();
        productService = new ProductServiceImpl();
        permissionService = new PermissionServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //利用反射获取要执行的servlet
            String operation = req.getParameter("operation");
            Class clazz = this.getClass();

            Method method = clazz.getDeclaredMethod(operation, HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            //获取开始时间
            long begin = System.currentTimeMillis();
            //执行servlet
            method.invoke(this, req, resp);
            //获取结束时间
            long end = System.currentTimeMillis();
            //如果执行增删改操作，则执行以下操作
            if ("list".equals(operation) ||
                    "view".equals(operation) ||
                    "toAdd".equals(operation) ||
                    "toEdit".equals(operation) ||
                    "findById".equals(operation)||
                    "findByUsername".equals(operation)
            ) {
                return;
            }
            //获取id，访问时间，访问IP，资源url，执行时间，执行方法
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Syslog syslog = new Syslog();
            syslog.setExecutionTime((int) (end - begin));
            syslog.setId(UUID.randomUUID().toString());
            syslog.setIp(req.getRemoteAddr());
            syslog.setUrl(req.getRequestURI());
            syslog.setVisitTime(simpleDateFormat.format(new Date()));
            syslog.setUsername("test");
            syslog.setMethod(operation);
            syslogService.save(syslog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
